package com.example.testnetwork.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.example.testnetwork.KeepLiveService;
import com.example.testnetwork.utils.MyLog;

/**
 * 接收启动广播，启动Service:KeepLiveService
 */
public class BootReceiver extends BroadcastReceiver {
    String TAG = "BootReceiver";
    private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        MyLog.d(TAG, "开机自启动.....");
        if(action.equals(ACTION_BOOT)) {
            if (Build.VERSION.SDK_INT >= 26) {
                context.startForegroundService(new Intent(context, KeepLiveService.class));
            } else {
                context.startService(new Intent(context, KeepLiveService.class));
            }
        }
    }
}
